﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ZGameFramework.UI
{
    internal sealed partial class UIManager : GameFrameworkModule, IUIManager
    {
        public class UIForm : IReference
        {
 
            private UIManager m_Manager;
            private IUIBehaivour m_Behaivour;
            private UIFormInfo m_FormInfo;
            private GameFrameworkParam m_Param;

            public IUIBehaivour Behaivour { get => m_Behaivour; }
            public UIFormInfo FormInfo { get => m_FormInfo; }
      
            /// <summary>
            /// 
            /// </summary>
            /// <param name="manager"></param>
            public void SetManager(UIManager manager)
            {
                m_Manager = manager;
            }

            /// <summary>
            /// 设置
            /// </summary>
            /// <param name="formInfo"></param>
            public void SetFormInfo(UIFormInfo formInfo)
            {
                m_FormInfo = formInfo;
            }

            /// <summary>
            /// 设置参数
            /// </summary>
            /// <param name="param"></param>
            public void SetParam(GameFrameworkParam param)
            {
                m_Param = param;
            }

            /// <summary>
            /// 
            /// </summary>
            public void LoadBehavior(System.Action<UIForm> onComplete)
            {
                GameObject objPanel = m_Manager.m_UIResourceManager.LoadPerfab(m_FormInfo.AssetPath);
                m_Behaivour = objPanel.GetComponent<UIBehaivour>();
                m_Behaivour.m_GameObject.name = m_FormInfo.Name;
                if (null != onComplete) { onComplete(this); }
            }

            /// <summary>
            /// 界面入场
            /// </summary>
            /// <returns></returns>
            public float FadeIn()
            {
                m_Behaivour.RegisterEvent();
                return m_Behaivour.FadeIn(m_Param);
            }

            /// <summary>
            /// 界面出场
            /// </summary>
            /// <returns></returns>
            public float FaderOut()
            {
                m_Behaivour.UnRegisterEvent();
                return m_Behaivour.FadeOut();
            }

            /// <summary>
            /// 入场结束
            /// </summary>
            public void FadeInAfter()
            {
                m_Behaivour.FadeInAfter();
            }

            /// <summary>
            /// 出场结束
            /// </summary>
            public void FadeOutAfter()
            {
                m_Behaivour.FadeOutAfter();
            }

            /// <summary>
            /// 清理回收
            /// </summary>
            public void Clear()
            {
                if ( null != m_Behaivour)
                {
                    m_Manager.m_UIResourceManager.ReleasePerfab( m_Behaivour.m_GameObject );
                }
                m_Manager = null;
                m_Behaivour = null;
                m_FormInfo = null;
            }
        }
    }
}
